//    Academic project for DOS, sorts numbers concurrently on 5 machines
//    Copyright (C) 2010  PRITHVI RAJ VENKAT RAJ
//
//       Licensed to the Apache Software Foundation (ASF) under one
//       or more contributor license agreements.  See the NOTICE file
//       distributed with this work for additional information
//       regarding copyright ownership.  The ASF licenses this file
//       to you under the Apache License, Version 2.0 (the
//       "License"); you may not use this file except in compliance
//       with the License.  You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
//       Unless required by applicable law or agreed to in writing,
//       software distributed under the License is distributed on an
//       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//       KIND, either express or implied.  See the License for the
//       specific language governing permissions and limitations
//       under the License.



import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.*;


public class Client {
	 public static void main(String args[]) throws InterruptedException {
		 Random generator= new Random();
		 
		 String arr="";
		 int port = 0;
		 String host=null,client_id = null;
		 if (args.length > 0) {
		     try {
		         port = Integer.parseInt(args[2]);
		         client_id = args[1];
		         host=args[0];
		     } catch (NumberFormatException e) {
		         System.err.println("Argument must be an integer");
		         System.exit(1);
		     }
		 }
		 
		 WriteLog out=new WriteLog(client_id);
		 out.log(" port = "+port+" host = "+host+"\n");
		 
	      ObjectOutputStream oos = null;
	      ObjectInputStream ois = null;

	      try {
	    	  
	        // open a socket connection
	        Socket socket = new Socket(host, port);
	        // open I/O streams for objects
	        oos = new ObjectOutputStream(socket.getOutputStream());
	        ois = new ObjectInputStream(socket.getInputStream());
	        // create serialized objects

	        SerializedObject result = new SerializedObject();
	        int outArray[] = new int[7];
	        
	        //set cpu speed
	       
	        int cpu=generator.nextInt(9)+1;
	        result.setCpu(cpu);
	        result.setClient_id(client_id);
	      
	        oos.writeObject(result);
	        oos.flush();
	        
	        
	        
	        result = (SerializedObject) ois.readObject();
//	        out.log("Got result");
	        outArray = result.getArray();
	        //System.out.print("Client"+client_id+" :Got  :");

	        out.log("CPUSPEED IS "+cpu+" and got  "+outArray.length+"  elements."+"\n");
	       
	        
	        arr="RECV:";
	        // printing array
	        for(int i=0;i<outArray.length;i++) {
	          arr+=outArray[i] + ",";
	        }
	        out.log(arr+"\n");
	       
//	        out.log("Client"+client_id+": CPUSPEED IS "+cpu+" and got"+outArray.length+"elements");
	        //sorting array
	        Arrays.sort(outArray);
	        
	        result.setCpu(cpu);
	        result.setClient_id(client_id);
	        result.setArray(outArray);
	        
	        //shipping array to server
	        oos.writeObject(result);
	        
	        arr="SENT:";
	        // printing array
	        for(int i=0;i<outArray.length;i++) {
	          arr+=outArray[i] + ",";
	        }
	        out.log(arr+"\n");
	        
	        out.log("BYE\n");
	        out.log("Terminated connection successfully\n");
	        
	        oos.flush();
	        oos.close();
	        ois.close();
	        out.close();
	      } catch(Exception e) {
	        out.log(e.getMessage());
	      }
	   }
	
}

 class WriteLog{
	FileWriter outFile;	
	PrintWriter out;
	String client_id;
	public	WriteLog(String client_id){
		this.client_id=client_id;
		try {
			outFile = new FileWriter(client_id+".log");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out = new PrintWriter(outFile);
	}
	
    
	public void log(String msg){
		System.out.print("Client ["+client_id+"]:"+msg);
		out.print("Client ["+client_id+"]:"+msg);
//		out.close();
	}
	public void close(){out.close();}
	
}
